//
//  ScenesFoaMixer.cpp
//  ScenesPlayer
//
//  Created by Branch on 2017/5/2.
//  Copyright © 2017年 Scenes. All rights reserved.
//

#include "ScenesFoaMixer.h"
#include "ScenesFoaBinauralizer.h"
#include "ScenesFoaRotator.h"

#ifdef _MSC_VER
#include <Assert.h>
#else
#include <assert.h>
#endif

ScenesFoaMixer::ScenesFoaMixer()
: _srcData(nullptr)
, _destData(nullptr)
, _binauralizer(nullptr)
, _nsamples(0)
{

}

ScenesFoaMixer::~ScenesFoaMixer(){


}

bool ScenesFoaMixer::openMixer(uint32_t nsamples){
    bool ret = false;
    _srcData = new float*[4];
    _destData = new float*[2];
    _nsamples = nsamples;
    _binauralizer = new ScenesFoaBinauralizer();
    ret = _binauralizer->open(nsamples);
    if (!ret) {
        closeMixer();
    }
    return ret;
}

void ScenesFoaMixer::closeMixer(){
    if (_binauralizer) {
        _binauralizer->close();
        delete _binauralizer;
        _binauralizer = nullptr;
    }
    if (_destData) {
        delete[] _destData;
        _destData = nullptr;
    }
    if (_srcData) {
        delete[] _srcData;
        _srcData = nullptr;
    }
    

    

}


bool ScenesFoaMixer::processAudio(float* dataW,float* dataY,float* dataZ,float* dataX,
                                  float* matrix4,float* dataL,float* dataR){
    assert(_binauralizer);
    bool ret = false;
    
    _srcData[0] = dataW;
    _srcData[1] = dataY;
    _srcData[2] = dataZ;
    _srcData[3] = dataX;
    
    _destData[0] = dataL;
    _destData[1] = dataR;

    ScenesFoaRotator::rotator(_srcData, matrix4,_nsamples);
    ret = _binauralizer->processAudio(_srcData, _destData);
    
    return ret;
}

